package com.xmtlxx.wms.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.xmtlxx.common.core.domain.PageQuery;
import com.xmtlxx.common.core.page.TableDataInfo;
import com.xmtlxx.common.utils.StringUtils;
import com.xmtlxx.wms.domain.StoreLog;
import com.xmtlxx.wms.domain.bo.StoreLogBo;
import com.xmtlxx.wms.domain.vo.StoreLogVo;
import com.xmtlxx.wms.mapper.StoreLogMapper;
import com.xmtlxx.wms.service.IStoreLogService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 仓库日志Service业务层处理
 *
 * @author xmtlxx
 * @date 2022-08-15
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class StoreLogServiceImpl implements IStoreLogService {

    private final StoreLogMapper baseMapper;

    /**
     * 查询仓库日志
     *
     * @param id 仓库日志主键
     * @return 仓库日志
     */
    @Override
    public StoreLogVo queryById(String id){
        //return baseMapper.selectVoById(id);
        return baseMapper.selectStoreLogVoByID(id);
    }

    /**
     * 查询仓库日志列表
     *
     * @param bo 仓库日志
     * @return 仓库日志
     */
    @Override
    public TableDataInfo<StoreLogVo> queryPageList(StoreLogBo bo, PageQuery pageQuery) {
       // LambdaQueryWrapper<StoreLog> lqw = buildQueryWrapper(bo);
       // Page<StoreLogVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        Page<StoreLogVo> result = baseMapper.selectPageStoreLogList(pageQuery.build(), bo);
        return TableDataInfo.build(result);
    }

    /**
     * 查询仓库日志列表
     *
     * @param bo 仓库日志
     * @return 仓库日志
     */
    @Override
    public List<StoreLogVo> queryList(StoreLogBo bo) {
       // LambdaQueryWrapper<StoreLog> lqw = buildQueryWrapper(bo);
       // return baseMapper.selectVoList(lqw);
        return baseMapper.selectStoreLogList(bo);
    }

    private LambdaQueryWrapper<StoreLog> buildQueryWrapper(StoreLogBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<StoreLog> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getDeptId() != null, StoreLog::getDeptId, bo.getDeptId());
        lqw.eq(StringUtils.isNotBlank(bo.getStoreId()), StoreLog::getStoreId, bo.getStoreId());
        lqw.eq(bo.getLogTime() != null, StoreLog::getLogTime, bo.getLogTime());
        lqw.eq(bo.getUserId() != null, StoreLog::getUserId, bo.getUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getOperator()), StoreLog::getOperator, bo.getOperator());
        lqw.eq(StringUtils.isNotBlank(bo.getLogType()), StoreLog::getLogType, bo.getLogType());
        lqw.eq(StringUtils.isNotBlank(bo.getCreateBy()), StoreLog::getCreateBy, bo.getCreateBy());
        lqw.between(params.get("beginCreateTime") != null && params.get("endCreateTime") != null,
            StoreLog::getCreateTime ,params.get("beginCreateTime"), params.get("endCreateTime"));
        lqw.eq(StringUtils.isNotBlank(bo.getDelFlag()), StoreLog::getDelFlag, bo.getDelFlag());
        return lqw;
    }

    /**
     * 新增仓库日志
     *
     * @param bo 仓库日志
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean insertByBo(StoreLogBo bo) {
        StoreLog add = BeanUtil.toBean(bo, StoreLog.class);
        validEntityBeforeSave(add);

        //填充UUID
        add.setId(IdUtil.simpleUUID());
        add.setDelFlag("0");
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改仓库日志
     *
     * @param bo 仓库日志
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateByBo(StoreLogBo bo) {
        StoreLog update = BeanUtil.toBean(bo, StoreLog.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(StoreLog entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除仓库日志
     *
     * @param ids 需要删除的仓库日志主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
